package cn.antscript.antLab.test 
{
	import com.flashdynamix.motion.TweensyZero;
	import com.actionsnippet.qbox.QuickBox2D;
	import com.actionsnippet.qbox.QuickObject;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;

	
	/**
	 * @author ant
	 */
	public class Box2dTest extends MovieClip 
	{

		private var _box2d : QuickBox2D;
		private var _box : QuickObject;
		private var _box2 : QuickObject;
		private var _box3 : QuickObject;
		private var _c : Sprite;

		private var _myS:MyB2Shape;
		
		
		
		public function Box2dTest() {
			//stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.frameRate=60;
			
			var l:Number=2*30;
			var cosL:Number=0.866*l;
			
			
			 

			var mo:MovieClip=new MovieClip();
			addChild(mo)
			_box2d=new QuickBox2D(mo,{debug:false});
			_box2d.createStageWalls();
			_box2d.timeStep=1/60.0;
			_box2d.setDefault({fillColor:0xff99aa})
			
			
			_c=new Sprite();
			_c.graphics.beginFill(0,0.4);
			_c.graphics.moveTo(0, -l);
			_c.graphics.lineTo(cosL, -l/2);
			_c.graphics.lineTo(cosL, l/2);
			_c.graphics.lineTo(0, l);
			_c.graphics.lineTo(-cosL, l/2);
			_c.graphics.lineTo(-cosL, -l/2);
			_c.graphics.lineTo(0, -l);
			_c.x=200;
			_c.y=200;
			addChild(_c);
			
			_box=_box2d.addBox({x:5,y:5,width:1,height:3,angle:0.3
			,restitution:.5,density:1});
			_box2d.addBox({x:5,y:5,width:1,height:3,angle:0.3
			,restitution:.5,density:1});
			_box2d.addBox({x:5,y:5,width:1,height:3,angle:0.3
			,restitution:.5,density:1});
			_box2d.addBox({x:5,y:5,width:1,height:3,angle:0.3
			,restitution:.5,density:1});
			_box2=_box2d.addCircle({x:5,y:5,radius:1,fillColor:0xff0099,lineColor:0x0000ff,lineAlpha:0,fillAlpha:0.5
			,restitution:1.1,density:1});
			_box3=_box2d.addPoly({x:10,y:5,verts:[[0,-l,cosL,-l / 2,cosL,l/ 2,0,l,-cosL,l / 2,-cosL,-l/2]],isBullet:true,fillColor:0xff0099,lineColor:0x0000ff,lineAlpha:0,fillAlpha:0.5
			,density:6});
			//_box.fullDestroy();
			new MyB2Shape(_box2d, 60,100,400);
			_myS=new MyB2Shape(_box2d, 30,200,300);
			//_box2d.
			//trace(_box)
			//_box.bodyDef.userData.scaleX=3;
			//_box2d.addJoint({a:_box.body, b:_box2.body,x1:9,y1:3,x2:10,y2:3,collideConnected:true})
			//_box2d.addGroup({objects:[_box,_box2,_box3], x:3, y:4})
			//_box2d.addEventListener(MouseEvent.MOUSE_OVER, onOver);

			_c.addEventListener(MouseEvent.ROLL_OVER, onOver)
			_c.addEventListener(MouseEvent.ROLL_OUT, onOut)
			
			//_box2d.addTimeStepSequence({time:100,callback:})
			
			//			_box2d.addJoint({type:"pulley", a:_box.body, b:_box2.body,groundAnchor1:new b2Vec2(2,2)
//			,groundAnchor2:new b2Vec2(3,3)})
			
				
			_box2d.start();
			_box2d.mouseDrag();
			
			addEventListener(Event.ENTER_FRAME, onEnter)
			
			TweensyZero.onUpdate=onUpdate;
		}
		
		private function onEnter(event : Event) : void
		{
			
		}

		private function onOut(event : MouseEvent) : void
		{			
			TweensyZero.to(_c,{scaleX:1,scaleY:1},1)
			
		}

		private function onOver(event : Event) : void
		{
			TweensyZero.to(_c,{scaleX:2,scaleY:2},1)
			_myS.destroy();
			_myS=new MyB2Shape(_box2d, _c.height / 2, _c.x, _c.y,false);
			
		}
		
		private function onUpdate():void
		{
			_myS.destroy();
			_myS=new MyB2Shape(_box2d, _c.height / 2, _c.x, _c.y,false);
			
		}
	}
}
